package com.wanqing.community.manager.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.wanqing.community.bean.RoleBean;
import com.wanqing.community.dao.RoleDao;
import com.wanqing.community.manager.RoleManager;
import com.wanqing.community.util.CommunityException;
import com.wanqing.community.util.ConditionParse;
import com.wanqing.community.util.StringTools;

/**
 * 角色管理业务逻辑实现
 */
@Service("roleManager")
public class RoleManagerImpl implements RoleManager{

	private static Logger log = Logger.getLogger(RoleManagerImpl.class);
	
	@Resource
	private RoleDao roleDao;
	
	/**
	 * 增加角色
	 * <p>Description:<p>
	 * <p>modifiContent:<p>
	 * @param role
	 * @return
	 * @throws CommunityException
	 */
	public boolean addRole(RoleBean role) throws CommunityException{
		int i = 0;
		try{
			i = this.roleDao.addRole(role);
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return (i>0)?true:false;
	}

	/**
	 * 删除角色
	 * <p>Description:<p>
	 * <p>modifiContent:<p>
	 * @param condParse
	 * @return
	 * @throws CommunityException
	 */
	public boolean deleteRole(List<String> list) throws CommunityException{		
		int i = 0;
		try{
			i = this.roleDao.deleteRole(list);
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return (i>0)?true:false;
	}

	/**
	 * 得到角色数
	 * <p>Description:<p>
	 * <p>modifiContent:<p>
	 * @param condParse
	 * @return
	 * @throws CommunityException
	 */
	public int getRoleCount(ConditionParse condParse) throws CommunityException{		
		int total = 0;
		try{
			total = this.roleDao.getRoleCount(condParse);
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return total;
	}

	/**
	 * 分页查询角色
	 * <p>Description:<p>
	 * <p>modifiContent:<p>
	 * @param condParse
	 * @param currPage
	 * @param pageSize
	 * @return
	 * @throws CommunityException
	 */
	public List<RoleBean> queryRole(ConditionParse condParse, int currPage,
			int pageSize) throws CommunityException{
		List<RoleBean> list = null;
		try{
			list = this.roleDao.queryRole(condParse, currPage, pageSize);
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return list;
	}

	/**
	 * 修改角色
	 * <p>Description:<p>
	 * <p>modifiContent:<p>
	 * @param role
	 * @return
	 * @throws CommunityException
	 */
	public boolean updateRole(RoleBean role) throws CommunityException{		
		int i = 0;
		try{
			i = this.roleDao.updateRole(role);
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return (i>0)?true:false;
	}

	/**
	 * <p>Description：批量添加角色权限<p>
	 * <p>modifiContent:<p>
	 * @param list
	 * @return
	 * @throws CommunityException
	 */
	public boolean addRoleAuthor(List<Map<String, Object>> list) throws CommunityException{		
		int sum = 0;
		int size = 0;
		if(null==list || list.isEmpty()){
			return false;
		}
		try{			
			Map<String, Object> map = list.get(0);
			String roleId = StringTools.objectToString(map.get("roleId"));
			if(StringTools.isEmptyOrNull(roleId)){
				return false;
			}
			this.roleDao.deleteFuncAuthor(roleId);
			for(int i = 0;i<list.size();i++){
				if((size%100==0 && size!=0) || size==(list.size()-1)){
					int row = this.roleDao.addRoleAuthorBatch(list);
					sum+=row;
				}
				size++;
			}
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return (sum>0)?true:false;
	}

	/**
	 * 
	 * <p>Description:查询角色对应的权限<p>
	 * <p>modifiContent:<p>
	 * @param roleId
	 * @return
	 * @throws CommunityException
	 */
	public List<RoleBean> queryOperaAuthorRole(String roleId) throws CommunityException{		
		List<RoleBean> list = null;
		try{
			list = this.roleDao.queryOperaAuthorRole(roleId);
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return list;
	}

	
	public List<RoleBean> listRole() throws CommunityException {
		List<RoleBean> list = null;
		try{
			list = this.roleDao.listRole();
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return list;
	}

}
